package com.example.pwj.ffstudy.ui.activity;

import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.example.pwj.ffstudy.R;
import com.example.pwj.ffstudy.base.BaseActivity;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyTitle;
import com.example.pwj.ffstudy.ui.fragment.DouFragment;
import com.example.pwj.ffstudy.ui.gank.view.GankFragment;
import com.example.pwj.ffstudy.ui.zhihu.view.ZhiFragment;
import com.example.pwj.ffstudy.util.EventBusUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.float_btn)
    FloatingActionButton mFloatBtn;
    @BindView(R.id.navi_view)
    NavigationView mNaviView;
    @BindView(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.rlContainer)
    RelativeLayout mRlContainer;
    private AnimationDrawable mAnimationDrawable;
    private ActionBarDrawerToggle mDrawerToggle;
    private Fragment mCurrentFragment;

    @Override
    protected void init() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }
        mAnimationDrawable = new AnimationDrawable();
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                mAnimationDrawable.stop();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                mAnimationDrawable.start();
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mNaviView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                Class<?extends Fragment> clazz = null;
                switch (item.getItemId()) {
                    case R.id.action_gank:
                        clazz = GankFragment.class;
                        break;
                    case R.id.action_zhihu:
                        clazz = ZhiFragment.class;
                        break;
                    case R.id.action_douban:
                        clazz = DouFragment.class;
                        break;
                    default:
                        break;
                }
                showAndHideFragment(getSupportFragmentManager(),R.id.rlContainer,clazz);
                return true;
            }
        });
        mFloatBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        showAndHideFragment(getSupportFragmentManager(),R.id.rlContainer,GankFragment.class);
        EventBusUtils.register(this);
    }

    private void showAndHideFragment(FragmentManager fm, int containerId, Class<? extends Fragment> clazz) {
        if (mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName())) {
            return;
        }
        try {
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragmentByTag = fm.findFragmentByTag(clazz.getSimpleName());
            if (fragmentByTag != null) {
                ft.show(fragmentByTag);
                if (mCurrentFragment != null) {
                    ft.hide(mCurrentFragment);
                }
            } else {
                fragmentByTag = clazz.newInstance();
                ft.add(containerId, fragmentByTag, clazz.getSimpleName());
                if (mCurrentFragment != null) {
                    ft.hide(mCurrentFragment);
                }
            }
            mCurrentFragment = fragmentByTag;
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(Gravity.START);
                break;
            case R.id.action_settings:
                ToastUtils.showShort("action_settings");
                break;
        }
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ZDailyTitle zDailyTitle){
        mToolbar.setTitle(zDailyTitle.getTitle());
    }
}
